﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace BingWallpaper
{

    public class Data
    {
        public string Startdate { get; set; }
        public string Fullstartdate { get; set; }
        public string Enddate { get; set; }
        public string Url { get; set; }
        public string Urlbase { get; set; }
        public string Copyright { get; set; }
        public string Copyrightlink { get; set; }
        public string Headline { get; set; }

        //静态数据
        readonly static string downPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "BingWallpaper");
        //静态数据构造函数
        static Data()
        {
            if (!Directory.Exists(downPath)) Directory.CreateDirectory(downPath);
        }

        /// <summary>
        /// 下载图片文件
        /// </summary>
        /// <returns>文件名</returns>
        public string GetImageFile()
        {
            try
            {
                //string downFormat = "&pid=hp&w={0}&h={1}&rs=1&c=4";
                //Size size = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Size;
                //var url = BingWallpaper.Url + Urlbase + string.Format(downFormat, size.Width, size.Height);
                var url = BingWallpaper.Url + Url;
                var path = Path.Combine(downPath, $"{Startdate}.jpg");
                if (!File.Exists(path) || new FileInfo(path).Length == 0) new WebClient().DownloadFile(url, path);
                return path;
            }
            catch (Exception)
            {
                return null;
            }
        }

    }
}
